Spot-On
spot-on-crypt.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Spot-On without specific prior written permission.
15 **
16 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef _spoton_crypt_h_
29 #define _spoton_crypt_h_
30 
31 #ifdef SPOTON_LINKED_WITH_LIBNTRU
32 extern "C"
33 {
34 #include "../../libNTRU/src/ntru.h"
35 }
36 #endif
37 
38 extern "C"
39 {
40 #include <errno.h>
41 #include <gcrypt.h>
42 #include <openssl/pem.h>
43 #include <openssl/rsa.h>
44 #include <openssl/ssl.h>
45 #include <openssl/x509v3.h>
46 #ifdef SPOTON_LINKED_WITH_LIBPTHREAD
47 #endif
48 }
49 
50 #include <QAtomicInt>
51 #include <QByteArray>
52 #include <QHostAddress>
53 #include <QMutex>
54 #include <QReadWriteLock>
55 #include <QSslCipher>
56 #include <QSslConfiguration>
57 #include <QStringList>
58 
59 #ifdef SPOTON_MCELIECE_ENABLED
60 class spoton_mceliece;
61 #endif
62 class spoton_threefish;
63 
65 {
66  public:
67  static QByteArray derivedSha1Key
68  (const QByteArray &salt,
69  const QString &passphrase,
70  const int hashKeySize,
71  const unsigned long int iterationCount);
72  static QPair<QByteArray, QByteArray> derivedKeys
73  (const QString &cipherType,
74  const QString &hashType,
75  const unsigned long int iterationCount,
76  const QString &passphrase,
77  const QByteArray &salt,
78  const bool singleIteration,
79  QString &error);
80  static QPair<QByteArray, QByteArray> derivedKeys
81  (const QString &cipherType,
82  const QString &hashType,
83  const unsigned long int iterationCount,
84  const QString &passphrase,
85  const QByteArray &salt,
86  const int hashKeySize,
87  const bool singleIteration,
88  QString &error);
89  static QByteArray fingerprint(const QByteArray &publicKey);
90  static QByteArray hash(const QByteArray &algorithm,
91  const QByteArray &data,
92  bool *ok);
93  static QByteArray keyedHash(const QByteArray &data,
94  const QByteArray &key,
95  const QByteArray &hashType,
96  bool *ok);
97  static QByteArray preferredHMAC(const QByteArray &data,
98  const QByteArray &key);
99  static QByteArray preferredHash(const QByteArray &data);
100  static QByteArray publicGPG(spoton_crypt *crypt);
101  static QByteArray publicKeyEncrypt(const QByteArray &data,
102  const QByteArray &pk,
103  const QByteArray &startsWith,
104  bool *ok);
105  static QByteArray saltedPassphraseHash(const QString &hashType,
106  const QString &passphrase,
107  const QByteArray &salt,
108  QString &error);
109  static QByteArray sha1FileHash(const QString &fileName);
110  static QByteArray sha1FileHash(const QString &fileName,
111  QAtomicInt &atomic);
112  static QByteArray sha1Hash(const QByteArray &data, bool *ok);
113  static QByteArray sha256Hash(const QByteArray &data, bool *ok);
114  static QByteArray sha3_512FileHash(const QString &fileName);
115  static QByteArray sha3_512FileHash(const QString &fileName,
116  QAtomicInt &atomic);
117  static QByteArray sha512Hash(const QByteArray &data, bool *ok);
118  static QByteArray shaXHash(const int algorithm,
119  const QByteArray &data,
120  bool *ok);
121  static QByteArray shake256(const QByteArray &buffer,
122  const size_t length,
123  bool *ok);
124  static QByteArray strongRandomBytes(const size_t size);
125  static QByteArray veryStrongRandomBytes(const size_t size);
126  static QByteArray weakRandomBytes(const size_t size);
127  static QByteArray whirlpoolHash(const QByteArray &data, bool *ok);
128  static QList<QSslCipher> defaultSslCiphers(const QString &sslControlString);
129  static QString publicKeyAlgorithm(const QByteArray &data);
130  static QString publicKeySize(const QByteArray &data);
131  static QString publicKeySizeMcEliece(const QByteArray &data);
132  static QString publicKeySizeNTRU(const QByteArray &data);
133  static QStringList buzzHashTypes(void);
134  static QStringList cipherTypes(void);
135  static QStringList congestionHashAlgorithms(void);
136  static QStringList hashTypes(void);
137  static bool exists(const QByteArray &publicKey, spoton_crypt *crypt);
138  static bool hasShake(void);
139  static bool isValidSignature(const QByteArray &data,
140  const QByteArray &publicKey,
141  const QByteArray &signature);
142  static bool memcmp(const QByteArray &bytes1, const QByteArray &bytes);
143  static bool passphraseSet(void);
144  static const char *preferredCipherAlgorithm(void);
145  static const char *preferredHashAlgorithm(void);
146  static const int SHA224_OUTPUT_SIZE_IN_BYTES = 28;
147  static const int SHA384_OUTPUT_SIZE_IN_BYTES = 48;
148  static const int XYZ_DIGEST_OUTPUT_SIZE_IN_BYTES = 64; /*
149  ** The preferred
150  ** digest algorithm's
151  ** output size.
152  */
153  static size_t cipherKeyLength(const QByteArray &cipherType);
154  static size_t ivLength(const QString &cipherType);
155  static void generateECCKeys(QByteArray &certificate,
156  QByteArray &privateKey,
157  QByteArray &publicKey,
158  QString &error,
159  const QHostAddress &address,
160  const int keySize,
161  const long int days);
162  static void generateMcElieceKeys(const QString &keySize,
163  QByteArray &privateKey,
164  QByteArray &publicKey,
165  bool *ok);
166  static void generateNTRUKeys(const QString &keySize,
167  QByteArray &privateKey,
168  QByteArray &publicKey,
169  bool *ok);
170  static void generateSslKeys(const int keySize,
171  QByteArray &certificate,
172  QByteArray &privateKey,
173  QByteArray &publicKey,
174  const QHostAddress &address,
175  const long int days,
176  QString &error);
177  static void init(const int secureMemorySize, const bool cbc_cts_enabled);
178  static void memcmp_test(void);
179 
180  static void memset_s(void *s, int c, size_t n)
181  {
182  if(!n || !s)
183  return;
184 
185  volatile auto v = static_cast<unsigned char *> (s);
186 
187  while(n--)
188  *v++ = (unsigned char) c;
189  }
190 
191  static void memzero(QByteArray &bytes);
192  static void memzero(QString &str);
193  static void purgeDatabases(void);
194  static void reencodePrivatePublicKeys
195  (spoton_crypt *newCrypt,
196  spoton_crypt *oldCrypt,
197  const QString &id,
198  QString &error);
199  static void removeFlawedEntries(spoton_crypt *crypt);
200  static void setSslCiphers(const QList<QSslCipher> &ciphers,
201  const QString &sslControlString,
202  QSslConfiguration &configuration);
203  static void terminate(void);
204  spoton_crypt(const QByteArray &privateKey,
205  const QByteArray &publicKey);
206  spoton_crypt(const QString &cipherType,
207  const QString &hashType,
208  const QByteArray &passphrase,
209  const QByteArray &symmetricKey,
210  const QByteArray &hashKey,
211  const int saltLength,
212  const unsigned long int iterationCount,
213  const QString &id);
214  spoton_crypt(const QString &cipherType,
215  const QString &hashType,
216  const QByteArray &passphrase,
217  const QByteArray &symmetricKey,
218  const QByteArray &hashKey,
219  const int saltLength,
220  const unsigned long int iterationCount,
221  const QString &id,
222  const QString &modeOfOperation);
223  spoton_crypt(const QString &cipherType,
224  const QString &hashType,
225  const QByteArray &passphrase,
226  const QByteArray &symmetricKey,
227  const int saltLength,
228  const unsigned long int iterationCount,
229  const QString &id);
230  ~spoton_crypt();
231  QByteArray decrypted(const QByteArray &data, bool *ok);
232  QByteArray decryptedAfterAuthenticated(const QByteArray &data, bool *ok);
233  QByteArray digitalSignature(const QByteArray &data, bool *ok);
234  QByteArray encrypted(const QByteArray &data, bool *ok);
235  QByteArray encryptedThenHashed(const QByteArray &data, bool *ok);
236  QByteArray hashKey(void);
237  QByteArray keyedHash(const QByteArray &data, bool *ok);
238  QByteArray publicKey(bool *ok);
239  QByteArray publicKeyDecrypt(const QByteArray &data, bool *ok);
240  QByteArray publicKeyHash(bool *ok);
241  QByteArray symmetricKey(void);
242  QPair<QByteArray, QByteArray> generatePrivatePublicKeys
243  (const QString &keySize,
244  const QString &keyType,
245  QString &error,
246  const bool save_keys = true);
247  QString cipherType(void) const;
248  QString publicKeyAlgorithm(void);
249  QString publicKeySize(void);
250  QString publicKeySizeMcEliece(void);
251  QString publicKeySizeNTRU(void);
252  bool isAuthenticated(void);
253  qint64 publicKeyCount(void);
254  void purgePrivatePublicKeys(void);
255 
256  private:
257  QAtomicInt m_isMcEliece;
258  QByteArray m_publicKey;
259  QMutex m_cipherMutex;
260  QReadWriteLock m_hashKeyMutex;
261  QReadWriteLock m_privateKeyMutex;
262  QReadWriteLock m_publicKeyMutex;
263  QReadWriteLock m_symmetricKeyMutex;
264  QString m_cipherType;
265  QString m_hashType;
266  QString m_id;
267  char *m_hashKey; // Stored in secure memory.
268  char *m_privateKey; // Stored in secure memory.
269  char *m_symmetricKey; // Stored in secure memory.
270  gcry_cipher_hd_t m_cipherHandle;
271  int m_cipherAlgorithm;
272  int m_hashAlgorithm;
273  int m_saltLength;
274  size_t m_hashKeyLength;
275  size_t m_privateKeyLength;
276  size_t m_symmetricKeyLength;
277 #ifdef SPOTON_MCELIECE_ENABLED
278  spoton_mceliece *m_mceliece;
279 #endif
280  spoton_threefish *m_threefish;
281  static QAtomicInt s_hasSecureMemory;
282  static bool s_cbc_cts_enabled;
283  unsigned long int m_iterationCount;
284  QByteArray publicKeyDecryptMcEliece(const QByteArray &data, bool *ok);
285  QByteArray publicKeyDecryptNTRU(const QByteArray &data, bool *ok);
286  void freeHashKey(void);
287  void freePrivateKey(void);
288  void freeSymmetricKey(void);
289  void init(const QString &cipherType,
290  const QString &hashType,
291  const QByteArray &passphrase,
292  const QByteArray &symmetricKey,
293  const QByteArray &hashKey,
294  const int saltLength,
295  const unsigned long int iterationCount,
296  const QString &id,
297  const QString &modeOfOperation);
298  void initializePrivateKeyContainer(bool *ok);
299  void setHashKey(const QByteArray &hashKey);
300  static QByteArray publicKeyEncryptMcEliece(const QByteArray &data,
301  const QByteArray &publicKey,
302  bool *ok);
303  static QByteArray publicKeyEncryptNTRU(const QByteArray &data,
304  const QByteArray &publicKey,
305  bool *ok);
306  static QByteArray randomBytes(const size_t size,
307  const enum gcry_random_level level);
308  static bool setInitializationVector(QByteArray &iv,
309  const int algorithm,
310  gcry_cipher_hd_t cipherHandle);
311  static void generateCertificate(const int keySize,
312  void *key,
313  QByteArray &certificate,
314  const QHostAddress &address,
315  const long int days,
316  QString &error);
317 };
318 
319 #endif
spoton_threefish
Definition: spot-on-threefish.h:34
spoton_crypt
Definition: spot-on-crypt.h:64